import { autoUpdater } from "electron-updater";
import { BrowserWindow } from 'electron'

export default function autoUpdate (win: BrowserWindow) {
  function sendStatusToWindow (text) {
    console.log(text);
    win.webContents.send('message', text);
  }

  autoUpdater.on('update-available', (info) => {
    sendStatusToWindow('检测到更新');
  })
  autoUpdater.on('error', (err) => {
    sendStatusToWindow('Error in auto-updater. ' + err);
  })
  autoUpdater.on('download-progress', (progressObj) => {
    let log_message = "Download speed: " + progressObj.bytesPerSecond;
    log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
    log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
    sendStatusToWindow(log_message);
  })
  autoUpdater.on('update-downloaded', (info) => {
    sendStatusToWindow('下载完成，重启即可自动更新');
  });
  autoUpdater.checkForUpdates()
}
